package com.softservecom.training.shop.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;

import com.softservecom.training.shop.pojos.UsersDetails;

import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * UsersDetails entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.softservecom.training.shop.pojos.UsersDetails
 * @author MyEclipse Persistence Tools
 */

public class UsersDetailsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UsersDetailsDAO.class);
	// property constants
	public static final String FIRST_NAME = "firstName";
	public static final String LAST_NAME = "lastName";
	public static final String ADDRESS = "address";
	public static final String _EMAIL = "EMail";
	public static final String MALE = "male";
	public static final String RATING = "rating";
	public static final String DESCRIPTION = "description";
	public static final String CITY_ID = "cityId";
	public static final String PHONE_ID = "phoneId";

	public void save(UsersDetails transientInstance) {
		log.debug("saving UsersDetails instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(UsersDetails persistentInstance) {
		log.debug("deleting UsersDetails instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UsersDetails findById(java.lang.Integer id) {
		log.debug("getting UsersDetails instance with id: " + id);
		try {
			UsersDetails instance = (UsersDetails) getSession().get(
					"com.softservecom.training.shop.beans.UsersDetails", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<UsersDetails> findByExample(UsersDetails instance) {
		log.debug("finding UsersDetails instance by example");
		try {
			List<UsersDetails> results = (List<UsersDetails>) getSession()
					.createCriteria(
							"com.softservecom.training.shop.beans.UsersDetails")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UsersDetails instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from UsersDetails as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<UsersDetails> findByFirstName(Object firstName) {
		return findByProperty(FIRST_NAME, firstName);
	}

	public List<UsersDetails> findByLastName(Object lastName) {
		return findByProperty(LAST_NAME, lastName);
	}

	public List<UsersDetails> findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List<UsersDetails> findByEMail(Object EMail) {
		return findByProperty(_EMAIL, EMail);
	}

	public List<UsersDetails> findByMale(Object male) {
		return findByProperty(MALE, male);
	}

	public List<UsersDetails> findByRating(Object rating) {
		return findByProperty(RATING, rating);
	}

	public List<UsersDetails> findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List<UsersDetails> findByCityId(Object cityId) {
		return findByProperty(CITY_ID, cityId);
	}

	public List<UsersDetails> findByPhoneId(Object phoneId) {
		return findByProperty(PHONE_ID, phoneId);
	}

	public List findAll() {
		log.debug("finding all UsersDetails instances");
		try {
			String queryString = "from UsersDetails";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public UsersDetails merge(UsersDetails detachedInstance) {
		log.debug("merging UsersDetails instance");
		try {
			UsersDetails result = (UsersDetails) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(UsersDetails instance) {
		log.debug("attaching dirty UsersDetails instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UsersDetails instance) {
		log.debug("attaching clean UsersDetails instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}